package com.diankangwo.controller;

import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.NewsDAO;
import com.diankangwo.domain.News;
import com.diankangwo.util.HtmlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 新闻接口
 *
 * @author xiaoyu
 */
@Controller
@Api(description = "新闻")
public class NewsController {

    @Autowired
    private NewsDAO newsDAO;


    /**
     * 分页查询新闻列表
     *
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return
     */
    @ApiOperation(value = "分页查询新闻列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "accessToken", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "分页参数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "分页参数", required = true, dataType = "Integer")
    })
    @ResponseBody
    @RequestMapping(value = "/news/list", method = RequestMethod.POST)
    public String findBylist(int pageNo, int pageSize, String type) {
        String code = "2";
        String msg = "处理异常！";
        List<News> nList = new ArrayList<News>();
        nList = newsDAO.findByList(pageNo, pageSize, type);
        if (nList != null) {
            code = "1";
            msg = "查询成功！";
            for (int i = 0; i < nList.size(); i++) {
                News news = new News();
                news = nList.get(i);
                if (HtmlUtil.getTextFromHtml(news.getContent()).length() > 140) {
                    nList.get(i).setContent(HtmlUtil.getTextFromHtml(news.getContent()).substring(0, 140));
                }
            }
        } else {
            code = "1";
            msg = "未查询到新闻！";
            return resultMsg.mapToJson(code, msg, nList);
        }
        return resultMsg.mapToJson(code, msg, nList);
    }

    /**
     * 查询总条数
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/news/num", method = RequestMethod.POST)
    public String num() {
        String code = "2";
        String msg = "处理异常！";
        List<News> nList = new ArrayList<News>();
        nList = newsDAO.findAll();
        if (nList != null) {
            code = "1";
            msg = "查询成功！";
        } else {
            code = "1";
            msg = "未查询到新闻！";
            return resultMsg.mapToJson(code, msg, nList.size());
        }
        return resultMsg.mapToJson(code, msg, nList.size());
    }

    /**
     * 按id查询新闻详情
     *
     * @param id 页码
     * @return
     */
    @ApiOperation(value = "按id查询新闻详情", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "新闻ID", required = true, dataType = "Integer"),
    })
    @ResponseBody
    @RequestMapping(value = "/news/findByid", method = RequestMethod.POST)
    public String findById(Integer id) {
        String code = "2";
        String msg = "处理异常！";
        News news = new News();
        code = "1";
        news = newsDAO.find(id);
        if (news != null) {
            return resultMsg.mapToJson(code, msg, news);
        } else {
            msg = "未查询到新闻！";
        }

        return resultMsg.mapToJson(code, msg, null);
    }
}
